//
//  SegView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/2.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "SegView.h"
#define IFXSTATU    (IS_NotIphoneX ? 40 : 64 )


@interface SegView ()
@property (nonatomic,strong)UISegmentedControl *segmentedControl;
@end

@implementation SegView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    UIView *segBaseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 41)];
    segBaseView.backgroundColor = [UIColor colorWithRed:0.14f green:0.52f blue:0.87f alpha:1.00f];
    UIImage *image;
    if ([[[UIDevice currentDevice] systemVersion] integerValue]>=9) {
         image = [[UIImage imageNamed:@"round"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    }else{
         image = [[UIImage imageNamed:@"roundBtn"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    }
    
//     UIImage *image = [[UIImage imageNamed:@"round"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [segBaseView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(segBaseView);
        make.width.height.mas_equalTo(segBaseView);
    }];
    [self addSubview:segBaseView];
    NSArray *segmentedArray = [NSArray arrayWithObjects:@"归属业主",@"车辆用途",@"车辆列表",nil];
    _segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedArray];
    [_segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    _segmentedControl.tintColor = [UIColor whiteColor];
    _segmentedControl.selectedSegmentIndex = 0;
    //选择后的字体颜色（在NSDictionary中 可以添加背景颜色和字体的背景颜色）
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0.13f green:0.47f blue:0.84f alpha:1.00f],
                         NSForegroundColorAttributeName,
                         [UIFont systemFontOfSize:BigFontSize],
                         NSFontAttributeName,nil];
    [_segmentedControl setTitleTextAttributes:dic forState:UIControlStateSelected];
    //默认字体颜色
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],
                          NSForegroundColorAttributeName,
                          [UIFont systemFontOfSize:BigFontSize],
                          NSFontAttributeName,nil];
    [_segmentedControl setTitleTextAttributes:dic1 forState:UIControlStateNormal];
    [segBaseView addSubview:_segmentedControl];
    [_segmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(kWidthOfPt(50));
        make.right.mas_equalTo(self.mas_right).offset(kWidthOfPt(-50));
        make.height.mas_equalTo(29);
        make.centerX.centerY.mas_equalTo(segBaseView);
    }];
}
//分栏点击事件
-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)seg{
    NSLog(@"seg.tag-->%ld",seg.selectedSegmentIndex);
    if (_segSelectIndex) {
        _segSelectIndex(seg.selectedSegmentIndex);
    }
}
- (void)segmentedControlDidSelectWithIndex:(NSInteger)index
{
    _segmentedControl.selectedSegmentIndex = index;
}

@end
